﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Menus.Model;
using Menus.Model.DB;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

using System.ComponentModel;


namespace MenusTestProject
{
    [TestClass]
    public class IngredientManager_test
    {
        [TestInitialize]
        public void Initialize()
        {
            Menus.Model.Globals.menusEF = new MenusEntity();
        }
        [TestMethod]
        public void IngredientManager_add_default()
        {
            //lance l'ajout de l'ingredient
            IngredientManager ingredient = new IngredientManager();
            string name = "ingredient test " + DateTime.Now.ToString();
            var newIngredient = ingredient.Add(name, Globals.coutNormal);

            //test la présence de l'ajout
            SqlConnection sqlCnx = new SqlConnection(ConfigurationManager.ConnectionStrings["TestProjectConnectionString"].ConnectionString);
            SqlCommand sqlCmd = new SqlCommand("select * from INGREDIENT where Libelle = '" + name + "'");
            sqlCmd.Connection = sqlCnx;
            sqlCnx.Open();
            SqlDataReader reader = sqlCmd.ExecuteReader();
            var res = reader.HasRows;
            reader.Close();

            //si ajout, on efface l'ajout
            if (res)
            {
                sqlCmd.CommandText = "DELETE FROM INGREDIENT where Libelle = '" + name + "'";
                sqlCmd.ExecuteNonQuery();
            }

            Assert.IsTrue(res,"L'insertion de l'ingrédient a échoué");
            Assert.IsTrue(newIngredient.ID > 0, "probleme avec le nouvel ingredient");
        }

        [TestMethod]
        public void IngredientManager_add_typed()
        {
            //lance l'ajout de l'ingredient
            IngredientManager ingredient = new IngredientManager();
            string name = "ingredient test " + DateTime.Now.ToString();
            ingredient.Add(name,2);

            //test la présence de l'ajout
            SqlConnection sqlCnx = new SqlConnection(ConfigurationManager.ConnectionStrings["TestProjectConnectionString"].ConnectionString);
            SqlCommand sqlCmd = new SqlCommand("select * from INGREDIENT where Libelle = '" + name + "'");
            sqlCmd.Connection = sqlCnx;
            sqlCnx.Open();
            SqlDataReader reader = sqlCmd.ExecuteReader();
            var res = reader.HasRows;
            reader.Close();

            //si ajout, on efface l'ajout
            if (res)
            {
                sqlCmd.CommandText = "DELETE FROM INGREDIENT where Libelle = '" + name + "'";
                sqlCmd.ExecuteNonQuery();
            }

            Assert.IsTrue(res, "L'insertion de l'ingrédient a échoué");
        }

        [TestMethod]
        public void IngredientManager_delete()
        {
            //ajout d'un ingredient directement en bdd
            string name = "ingredient test " + DateTime.Now.ToString();
            AddIngredient(name);

            //exécution de la suppression
            IngredientManager ingredient = new IngredientManager();
            ingredient.delete(name);

            //test si l'élément est là
            SqlConnection sqlCnx = new SqlConnection(ConfigurationManager.ConnectionStrings["TestProjectConnectionString"].ConnectionString);
            sqlCnx.Open();
            SqlCommand sqlCmd = new SqlCommand("select * from ingredient where libelle = '" + name + "' ", sqlCnx);
            SqlDataReader reader = sqlCmd.ExecuteReader();            

            //test true, ok, si le delete a fonctionné
            var res = reader.HasRows;
            reader.Close();

            if (res)
                DeleteIngredient(name);

            Assert.IsFalse(res, "La suppression de l'ingredient a échoué");       
        }

        [TestMethod]
        public void IngredientManager_load()
        {
            //ajout d'un ingredient directement en bdd
            string name = "ingredient test " + DateTime.Now.ToString();
            AddIngredient(name);

            IngredientManager ingredientManager = new IngredientManager();
            var ingredient = ingredientManager.load(name);
            var res = ingredient.Libelle == name;
            DeleteIngredient(name);

            Assert.IsTrue(res, "load ingredient ko");
        }

        [TestMethod]
        public void IngredientManager_exists()
        {
            string name = "ingredient test " + DateTime.Now.ToString();

            try
            {
                //on ajoute la donnée de test
                AddIngredient(name);
                IngredientManager ingredientManager = new IngredientManager();                
                var res = ingredientManager.Exists(name);
                var res2 = ingredientManager.Exists("toto");

                Assert.IsTrue(res, "ingredientManager -> exists ko1");
                Assert.IsFalse(res2, "ingredientManager -> exists ko2");
            }
            finally
            {
                //on supprime les données de test
                DeleteIngredient(name);
            }
        }

        [TestMethod]
        public void IngredientManager_GetListIngredient()
        {
            IngredientManager manager = new IngredientManager();
            var binding = manager.GetListIngredient();
            Assert.IsTrue(binding != null);
            Assert.IsTrue(binding.Count > 0);            
        }

        private void DeleteIngredient(string libelle)
        {
            SqlConnection sqlCnx = new SqlConnection(ConfigurationManager.ConnectionStrings["TestProjectConnectionString"].ConnectionString);
            sqlCnx.Open();
            SqlCommand sqlCmd = new SqlCommand("delete from ingredient where libelle = '"+libelle+"'", sqlCnx);
            sqlCmd.ExecuteNonQuery();
            sqlCnx.Close();
        }

        private void AddIngredient(string libelle)
        {
            SqlConnection sqlCnx = new SqlConnection(ConfigurationManager.ConnectionStrings["TestProjectConnectionString"].ConnectionString);
            sqlCnx.Open();
            SqlCommand sqlCmd = new SqlCommand("insert into ingredient (libelle, IngredientType_Id) values ('" + libelle + "', 1)", sqlCnx);
            sqlCmd.ExecuteNonQuery();
            sqlCnx.Close();
        }


    }
}

